<?php
$templatefile = "includes/template.html";
include "includes/vars.php";
include "includes/inc_view.php";
$link = mysql_connect($dbhost, $dbuser, $dbpass)
or Die("Cannot connect to database server");

mysql_select_db($dbname)
  or Die("Database not found.");


function getDwonloadBookLink1($link){
	return '<A HREF="index.php?act=download&BID=' . $link . '"><img src="images/downloadThisBook.gif" BORDER=0></A>';
}

// content management

if (isset($_GET['bkID'])){
//view chapter list
    if (!isset($_GET['chapter'])){
    
          // get the book's name
          $sql = "SELECT B_NAME, B_DOWNLOAD_COUNT, B_IMAGE_URL FROM books WHERE B_ID = " . $_GET['bkID'];
          if ($result = mysql_query($sql)){

            $row = mysql_fetch_row($result);
            $bookname = $row[0];
			$bookimage = '<img border="0" width="100" height="150" src="' . $websitename . $row[2] . '"><br>';
			$bookdownload = getDwonloadBookLink1($_GET['bkID']);

			$newcount = $row[1] + 1;
            $sql = "UPDATE books SET B_DOWNLOAD_COUNT = '" . $newcount . "' WHERE B_ID = " . $_GET['bkID'];
            $result = mysql_query($sql);
          }
          else{
              $bookname = "";
          }
      
          // print chapters list
          $sql = "SELECT CHAP_ID, CHAP_NAME FROM chapters WHERE CHAP_BK_ID = " . $_GET['bkID'] . " ORDER BY CHAP_SEQUENCE";

          if ($result = mysql_query($sql)){
              $contenttable = "";
              while ($row = mysql_fetch_row($result)){

                  $contenttable .= '<a href="view.php?bkID=' . $_GET['bkID'] . '&chapter=' . $row[0] . '">' . $row[1] . '</a><BR>';


              }

          }
          else{
              $content = "Sorry, could not get this book's information";
          }
     
      
    }//end of chapter list view
    else{
//view chapter content    

          // get the book's name
          $sql = "SELECT B_NAME, B_DOWNLOAD_COUNT, B_IMAGE_URL FROM books WHERE B_ID = " . $_GET['bkID'];
          if ($result = mysql_query($sql)){

            $row = mysql_fetch_row($result);
            $bookname = $row[0];			
			$bookimage = '<img border="0" width="100" height="150" src="' . $websitename . $row[2] . '"><br>';			
			$bookdownload = getDwonloadBookLink1($_GET['bkID']);
		  }
          else{
              $bookname = "";
          }


        //print the chapter in the inline frame
        $sql = "SELECT CHAP_URL  FROM chapters WHERE CHAP_ID = " . $_GET['chapter'] . " AND CHAP_BK_ID = " . $_GET['bkID'];
        if ($result = mysql_query($sql)){
        
            $row = mysql_fetch_row($result);
            
            // check the first letter
            $firstletter = substr($row[0], 0, 1);
            if ($firstletter == "/"){
                $urllength = strlen($row[0]);
                $chapterurl = substr($row[0], 1, $urllength);
            }
            else{
                $chapterurl = $row[0];
            }
            
                                      // get the file and extract the body --------------------------------------------------
                                      $filename = $chapterurl;

                                      $filetext = implode("", file($filename));

                                      $filetext = eregi_replace ("<body", "IBREPLACEMENT", $filetext);
                                      $filetext = eregi_replace ("</body", "IBREPLACEMENT", $filetext);

                                      $fileparts = explode("IBREPLACEMENT", $filetext);




                                      $fileparts2 = split (">", $fileparts[1], 2);



                                      $contenttable = $fileparts2[1];
            
            
            
            
            
            
            
            
            
            
            //$contenttable = '<iframe width="' . $iframewidth . '" height="' . $iframeheight . '" src="' . $websitename . $row[0] . '"></iframe>';

        
        }
        else{
            $content = "Could not retrieve this chapter";
        }
        

       //prepare the next, previous and content links
        // content
        $tblofcontentlnk = '<A HREF="view.php?bkID=' . $_GET['bkID'] . '"><img src="' . $websitename . '/images/' . $contentimage . '" border="0"></A>';
        //next link

        $nextlink = "";
        $sql = "SELECT CHAP_ID, CHAP_SEQUENCE FROM chapters WHERE CHAP_BK_ID = " . $_GET['bkID'] . " ORDER BY CHAP_SEQUENCE";
        if ($result = mysql_query($sql)){
            $nextfound = "no";
            $chapterfound = "no";
            while ($row = mysql_fetch_row($result)){


                if ($chapterfound == "yes"){
                    $nextfound = "yes";
                    $nextchapterID = $row[0];
                    $chapterfound = "no";
                }
                
                if ($row[0] == $_GET['chapter']){
                    $chapterfound = "yes";
                }


            }
            
            if ($nextfound == "yes"){
                $nextlink = '<A HREF="view.php?bkID=' . $_GET['bkID'] . '&chapter=' . $nextchapterID . '"><img src="' . $websitename . '/images/ib_next.gif" border="0"></A>';
            }
        }
        
        
        
        // previous link
        $prevlink = "";
        if ($result = mysql_query($sql)){
            $prevfound = "no";
            $stop = "no";
            while ($row = mysql_fetch_row($result)){



                if ($row[0] == $_GET['chapter']){
                    $stop = "yes";
                }

                if ($row[0] != $_GET['chapter'] && $stop != "yes"){
                    $prevfound = "yes";
                    $prevID = $row[0];
                }

            
            }
            
            if ($prevfound == "yes"){
                $prevlink = '<A HREF="view.php?bkID=' . $_GET['bkID'] . '&chapter=' . $prevID . '"><img src="' . $websitename . '/images/ib_previous.gif" border="0"></A>';
            }

        }

        $navigation = $prevlink . ' ' . $tblofcontentlnk . ' ' . $nextlink ;
                   // print chapters list
          $sql = "SELECT CHAP_ID, CHAP_NAME FROM chapters WHERE CHAP_BK_ID = " . $_GET['bkID'] . " ORDER BY CHAP_SEQUENCE";

          if ($result = mysql_query($sql)){
              $chapterlist = "";
              while ($row = mysql_fetch_row($result)){

                  $chapterlist .= '<a href="view.php?bkID=' . $_GET['bkID'] . '&chapter=' . $row[0] . '">' . $row[1] . '</a><BR>';


              }           }
    
    }//end of chapter content view
        $authsql ="SELECT BA_AUTH_ID FROM bk_auth WHERE BA_BOOK_ID = " . $_GET['bkID'];
    if ($authresult = mysql_query($authsql)){
        $authrow = mysql_fetch_row($authresult);
        $authsql2 ="SELECT * FROM authors WHERE A_ID = " . $authrow[0];
        if($authresult2 = mysql_query($authsql2)){
            $authrow2 = mysql_fetch_row($authresult2);
        }		if($authrow2[2]){
			$authorname .= 'By: <a href="view.php?authID=' . $authrow2[0] . '">' . $authrow2[1] . " " . $authrow2[2] . '</a><BR></P>';
		}    }      
   
    
    // replace the tags
    if (isset($_GET['chapter'])){
        $chapterview = str_replace("<!--NAVIGATION-->", $navigation, $chapterview);		$sidebar = str_replace("<!--CHAPTERLIST-->", $chapterlist, $sidebar);
    }
    $chapterview = str_replace("<!--CONTENT-->", $contenttable, $chapterview);
    $chapterview = str_replace("<!--BOOKNAME-->", $bookname, $chapterview);
    $content = $chapterview;
    $sidebar = str_replace("<!--BOOKIMAGE-->", $bookimage, $sidebar);    $sidebar = str_replace("<!--BOOKDOWNLOAD-->", $bookdownload, $sidebar);    $sidebar = str_replace("<!--AUTHORNAME-->", $authorname, $sidebar);
}



// list author books

else if(isset($_GET['authID'])){

    // get author ID and find books written and list alphabetically
    $sql = "SELECT BA_BOOK_ID FROM bk_auth WHERE BA_AUTH_ID = " . $_GET['authID'] . ' LIMIT ';

    if (!isset($_GET['page'])){
        $sql .= '0';
    }
    else{
        $sql .=  (($_GET['page']-1)*$bktoshownum);
    }
    
    $sql .= ", " . $bktoshownum;

    
    
    
    
    $bookslist = "";
    if ($result = mysql_query($sql)){
    
        while ($row = mysql_fetch_row($result)){

            // extract the book's information and display the list
            if (trim($row[0] != "")){
            
                $bsql = "SELECT B_NAME,  B_IMAGE_URL from books WHERE B_ID = " . $row[0];
                if ($bresult = mysql_query($bsql)){
                    $brow = mysql_fetch_row($bresult);

                    if (trim($brow[1]) != ""){
                        $bookslist .= '<p><a href="view.php?bkID=' . $row[0] . '"><img border="0" width="100" height="150" src="' . $websitename . $brow[1] . '"><br>';
                        $bookslist .= $brow[0] . '</a><br>';                                                
                        $bookslist .= getDwonloadBookLink1($row[0]);
                    }
                }
                
            }
            
            

        }
    
    }
    else{
        $bookslist = "Could not retrieve this author's books list.";
    }
    
    
    
            $sql = "SELECT BA_BOOK_ID FROM bk_auth WHERE BA_AUTH_ID = " . $_GET['authID'];
            if ($result = mysql_query($sql)){
                $numrecords = mysql_num_rows($result);

            }
            else{
                $numrecords = 5;
            }

            $numpages = round($numrecords/$bktoshownum);

            if (($numpages-($numrecords/$bktoshownum))<0){

                $numpages += 1;
            }

            if (!isset($_GET['page'])){
                $pagenumvalue = 1;
            }
            else{
                $pagenumvalue = $_GET['page'];
            }


            $pagenum = "page " . $pagenumvalue . " of " . $numpages;

            $prevlink = "";
            
            if ($pagenumvalue > 1){
                $prevlink = '<A HREF="view.php?authID=' . $_GET['authID'] . '&page=' . ($pagenumvalue-1) . '"><img src="' . $websitename . '/images/ib_previous.gif" border="0"></A>'  ;
            }


            $nextlink = "";
            if ($numpages > $pagenumvalue){
                $nextlink = '<A HREF="view.php?authID=' . $_GET['authID'] . '&page=' . ($pagenumvalue+1) . '"><img src="' . $websitename . '/images/ib_next.gif" border="0"></A>'  ;
            }
            
            
            $bookslist .= '<P align="right">' . $prevlink . ' ' . $pagenum . ' '. $nextlink . '</P>';


    
    $authorname = "";
    // get the author's name
    $asql = "SELECT * FROM authors WHERE A_ID = " . $_GET['authID'];
    if ($aresult = mysql_query($asql)){

    
        $arow = mysql_fetch_row($aresult);
        $authorname = $arow[1] . ' ' . $arow[2];
    
    }
    
    

    
    $authorview = str_replace("<!--AUTHORNAME-->", $authorname, $authorview);

    $authorview = str_replace("<!--CONTENT-->", $bookslist, $authorview);
    $content = $authorview;
    



}
else if($_GET['act']=="about"){

                                      $filename = $websitename . $abouturl ;

                                      $filetext = implode("", file($filename));

                                      $filetext = eregi_replace ("<body", "IBREPLACEMENT", $filetext);
                                      $filetext = eregi_replace ("</body", "IBREPLACEMENT", $filetext);

                                      $fileparts = explode("IBREPLACEMENT", $filetext);




                                      $fileparts2 = split (">", $fileparts[1], 2);



                                      $content = $fileparts2[1];

}
else if($_GET['act']=="downloads"){

                                      $filename = $websitename . $downloadsurl ;

                                      $filetext = implode("", file($filename));

                                      $filetext = eregi_replace ("<body", "IBREPLACEMENT", $filetext);
                                      $filetext = eregi_replace ("</body", "IBREPLACEMENT", $filetext);

                                      $fileparts = explode("IBREPLACEMENT", $filetext);




                                      $fileparts2 = split (">", $fileparts[1], 2);



                                      $content = $fileparts2[1];

}


else{

$content ='
<script language="javascript">
<!--

document.location="index.php";


-->
</script>
';


}









// prepare and send final file

if (file_exists($templatefile)){
    $output = implode ("", file($templatefile));
    $output = str_replace("<!--CONTENT-->", $content, $output);    $output = str_replace("<!--SIDEBAR-->", $sidebar, $output);
    print $output;

    
}
else{
    print "ERROR";
}


mysql_close ($link);

?>
